package com.paas.common.servlet.upload;


import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.Properties;

/**
 * Created by Alige on 2016/5/5.
 */
public class ServletFileManger {


    private final static String config_path = "upload.properties";

    private static String http_url;
    private static String upload_path;
    private static String nginx_img_url;
    private static String upload_img_path;
    private static int cache;
    Properties properties = null;


    public ServletFileManger() {
        properties = new ReadProperties().getProperties(config_path);
        http_url = properties.getProperty("nginx_address");
        upload_path = properties.getProperty("upload_path");
        nginx_img_url = properties.getProperty("nginx_img_url");
        upload_img_path = properties.getProperty("upload_img_path");
        String cache_str = properties.getProperty("cache");
        if (null != cache_str && !"".equals(cache_str)) {
            cache = Integer.parseInt(cache_str);
        }
    }


    public String getDownloadUrl() {
        return http_url;
    }


    private String write(BufferedInputStream bufferedInputStream, String outPath) throws IOException {
        BufferedOutputStream bufferedOutputStream = null;
        bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(outPath));
        byte[] bytes = new byte[cache];
        try {
            int count = 0;
            while (-1 != (count = bufferedInputStream.read(bytes))) {
                bufferedOutputStream.write(bytes, 0, count);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            bufferedOutputStream.flush();
            bufferedOutputStream.close();
            bufferedInputStream.close();
        }
        return outPath;
    }


    


    private boolean isImage(File file) {
        boolean flag = false;
        try {
            BufferedImage is = ImageIO.read(file);
            if (null == is) {
                return flag;
            }
            flag = true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

}